<?php
/**
 * Created by PhpStorm.
 * User: deng
 * Date: 2015/2/23
 * Time: 11:29
 */

namespace Lab\Controller;

use \MongoClient;
use Common\Controller\RestFulApiController;

class ModuleController extends RestFulApiController
{

    private $_modules = [
        'notices' => 'notices',
        'conferences' => 'conferences',
        'teachers' => 'teachers',
        'students' => 'students',
        'schoolfellows' => 'schoolfellows',
        'directions' => 'directions',
        'papers' => 'papers',
        'patents' => 'patents',
        'awards' => 'awards',
        'projects' => 'projects',
        'instruments' => 'instruments',
        'services' => 'services',
        'processes' => 'processes',
        'experiments' => 'experiments',
        'rules' => 'rules',
        'recruits' => 'recruits'
    ];

    private $_labId;

    public function __construct()
    {
        parent::__construct();
        $this->_model = $this->_db->labs;
        $this->_labId = $_GET['labId'];
        $moduleName = $_GET['moduleName'];
        if (array_key_exists($moduleName, $this->_modules)) {
            $module = $this->_modules[$moduleName];
            $this->_model = $this->_model->$module;
        } else
            ClientError($message = C('CODE.NOT_FOUND')['msg'], $status = C('CODE.NOT_FOUND')['code']);
        $this->init();
    }

    public function multiple()
    {
        switch ($this->_method) {
            case 'get':
                return $this->index();
                break;
            case 'post':
                return $this->create();
                break;
            case 'put':
                E(C('CODE.METHOD_NOT_ALLOWED')['msg'], $status = C('CODE.METHOD_NOT_ALLOWED')['code']);
                break;
            case 'delete':
                E(C('CODE.METHOD_NOT_ALLOWED')['msg'], $status = C('CODE.METHOD_NOT_ALLOWED')['code']);
                break;
            case 'head':
                break;
            case 'options':
                return $this->options('Access-Control-Allow-Methods: GET, POST, OPTIONS');
                break;
            default:
                E(C('CODE.METHOD_NOT_ALLOWED')['msg'], $status = C('CODE.METHOD_NOT_ALLOWED')['code']);
        }
    }

    public function single()
    {
        $id = $_REQUEST['moduleId'];
        switch ($this->_method) {
            case 'get':
                return $this->view($id);
                break;
            case 'post':
                E(C('CODE.METHOD_NOT_ALLOWED')['msg'], $status = C('CODE.METHOD_NOT_ALLOWED')['code']);
                break;
            case 'put':
                return $this->update($id);
                break;
            case 'delete':
                return $this->delete($id);
                break;
            case 'head':
                break;
            case 'options':
                return $this->options('Access-Control-Allow-Methods: GET, OPTIONS, PUT, DELETE');
                break;
            default:
                E(C('CODE.METHOD_NOT_ALLOWED')['msg'], $status = C('CODE.METHOD_NOT_ALLOWED')['code']);
                break;
        }
    }

    public function index()
    {
        $cond = array_merge(['labId' => $this->_labId], $this->_cond);
        $conf = [
            'fields' => $this->_fields,
            'cond' => $cond,
            'orderBy' => $this->_order,
            'skip' => $this->_skip,
            'limit' => $this->_limit
        ];
        if ($this->_action == 'count')
            $result['total'] = $this->_model->findBy($conf)['total'];
        else if ($this->_action == 'list')
            $result = $this->_model->findBy($conf)['list'];
        Success(C('CODE.OK')['code'], $result);
    }

    public function create()
    {
        $this->checkAuth();
        $data = $this->_data;
        $data['labId'] = $this->_labId;
        $result = $this->_model->create($data);
        Success(C('CODE.OK')['code'], $this->_model->find($result));
    }

    public function checkAuth()
    {
        /**
         * lab下的module里，还需要检查labId是否和token中的一致
         */
        parent::checkAuth();
        if ($this->_labId) {
            $labModel = $this->_db->labs;
            if ($labModel->find($this->_labId)['creatorId'] != $this->_token['userId'])
                ClientError('请求权限不足', c('CODE.UNAUTHORIZED')['code']);
        }
    }
}